package com.desheng.Controller;


import com.desheng.Service.BookService;
import com.desheng.Service.OrderService;
import com.desheng.Service.UserService;
import com.desheng.bean.BuyerItem;
import com.desheng.bean.Item;
import com.desheng.bean.SellerItem;
import com.desheng.bean.User;
import com.desheng.model.StatusCode;
import com.easyFramework.annotation.Autowired;
import com.easyFramework.annotation.Controller;
import com.easyFramework.annotation.RequestMapping;
import com.easyFramework.annotation.RequestParam;
import com.easyFramework.bean.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yezl
 * @date 2018/6/4 15:02
 * @decription this is :
 */
//@Controller("order")
@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    BookService bookService;


    @RequestMapping(value = "preparePay.do",method = "get")
    public ModelAndView preparePay(@RequestParam("itemid") Integer itemids, HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        Long userid = ((User) httpSession.getAttribute("user")).getUserid();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("pay.jsp");
        modelAndView.addModel("user", userService.getUser(userid));
        List<Item> items = new ArrayList<Item>();

        System.out.println("itemid:" + itemids);
        int[] itemIdList = new int[1];
        itemIdList[0] = itemids;
        if (orderService.canbebuied(itemIdList)) {
            for (int i : itemIdList) {
                Item item = bookService.getBook(i);
                items.add(item);
                orderService.deleteshopcart(i, userid);
            }
            modelAndView.addModel("itemlist", items);
            modelAndView.addModel("sum", orderService.sumPrice(itemIdList));
            modelAndView.getModel().put("flag", 200);
        } else
            modelAndView.getModel().put("flag", 400);
        return modelAndView;
    }

    @RequestMapping(value = "pay.do",method = "post")
    public ModelAndView pay(@RequestParam("itemidlist") Integer itemids, HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        User buyer = (User) httpSession.getAttribute("user");
        System.out.println("itemid:" + itemids);
        int[] itemidlist = new int[1];
        itemidlist[0] = itemids;
        for (int i : itemidlist)
            System.out.println("itemidlist" + i);
        int sum = orderService.sumPrice(itemidlist);
        StatusCode statusCode = orderService.accountcash(itemidlist, buyer, sum);
        httpSession.setAttribute("user", userService.getUser(buyer.getUserid()));

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("successPay.jsp");
        modelAndView.getModel().put("sum", sum);
        modelAndView.addModel("statusCode", statusCode);
        modelAndView.addModel("buyer", buyer);
        return modelAndView;

    }

    @RequestMapping("sellinfo")
    public ModelAndView sellinfo(HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        User seller = (User) httpSession.getAttribute("user");

        List<BuyerItem> itemList0 = orderService.getsellinfo(seller, 0);
        List<BuyerItem> itemList1 = orderService.getsellinfo(seller, 1);
        List<BuyerItem> itemList2 = orderService.getsellinfo(seller, 2);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("sellinfo.jsp");
        modelAndView.addModel("buyeritemList0", itemList0);
        modelAndView.addModel("buyeritemList1", itemList1);
        modelAndView.addModel("buyeritemList2", itemList2);
        return modelAndView;

    }

    @RequestMapping("order")
    public ModelAndView order(HttpServletRequest request) {
        HttpSession httpSession = request.getSession();
        User seller = (User) httpSession.getAttribute("user");

        List<SellerItem> sellitemList1 = orderService.getorderinfo(seller, 1);
        List<SellerItem> sellitemList2 = orderService.getorderinfo(seller, 2);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("order.jsp");
        modelAndView.addModel("sellitemList1", sellitemList1);
        modelAndView.addModel("sellitemList2", sellitemList2);
        return modelAndView;

    }

    @RequestMapping("shopcart")
    public ModelAndView shopcart(HttpServletRequest request) {
        System.out.println("shopcart.do");
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        List<Item> itemList = orderService.getshopcart(user);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("shopcart.jsp");
        modelAndView.addModel("itemList", itemList);

        return modelAndView;

    }
    @RequestMapping(value = "addShopCart.do")
    public void addShopCart(@RequestParam("itemid2") Integer itemids2, HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        StatusCode sCode = orderService.addshopcart(itemids2,user.getUserid());
        List<Item> itemList = orderService.getshopcart(user);
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.setPath("shopcart.jsp");
//        modelAndView.addModel("itemList", itemList);
//        request.setAttribute("itemList", itemList);
        response.sendRedirect("../order/shopcart");
//        return modelAndView;
    }
    @RequestMapping(value = "deleteShopCart.do")
    public void deleteShopCart(@RequestParam("itemid") Integer itemids, HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("deleteShopCart.do");
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        orderService.deleteshopcart(itemids,user.getUserid());
        response.sendRedirect("../order/shopcart");
    }

    @RequestMapping("deleteallshopcart.do")
    public ModelAndView deleteAllShopCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("deleteallshopcart.do");
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        orderService.deleteallshopcart(user.getUserid());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setPath("shopcart.jsp");
        modelAndView.addModel("itemList", null);
//        response.sendRedirect("../order/shopcart");
        return modelAndView;
    }
    @RequestMapping("confirmGood.do")
    public void confirmGood( HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        orderService.confirmgood(Integer.parseInt(request.getParameter("itemid")),user.getUserid());
        response.sendRedirect("../order/order");
    }
    @RequestMapping("deleteOrder.do")
    public void deleteOrder(HttpServletRequest request,HttpServletResponse response) throws IOException {
        System.out.println("deleteOrder.do");
        int itemids = Integer.parseInt(request.getParameter("itemid2"));
        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("user");
        orderService.deleteorder(itemids,user.getUserid());
        response.sendRedirect("../order/order");
    }

}
